{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-04T09:15:10.231239Z",
     "start_time": "2025-03-04T09:15:10.224989Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "import random\n",
    "import time"
   ],
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:49:30.079853Z",
     "start_time": "2025-03-04T08:48:38.279871Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = []\n",
    "for _ in range(10 ** 8):\n",
    "    a.append(random.random())\n",
    "print('-' * 50)\n",
    "t1 = time.time()\n",
    "sum1 = sum(a)\n",
    "t2 = time.time()\n",
    "\n",
    "b = np.array(a)\n",
    "\n",
    "t4 = time.time()\n",
    "sum3 = np.sum(b)\n",
    "t5 = time.time()\n",
    "print(t2 - t1, t5 - t4)"
   ],
   "id": "7d241222a43cd9ac",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "1.505725622177124 0.5660548210144043\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.1 创建一维数组",
   "id": "c8a2c04ca167f43d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:49:30.107026Z",
     "start_time": "2025-03-04T08:49:30.084848Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list1 = [1, 2, 3, 4]\n",
    "oneArray = np.array(list1)\n",
    "print(list1)\n",
    "print(type(oneArray))  # 对象类型\n",
    "print(oneArray)\n",
    "\n",
    "# 创建数组的多种形式\n",
    "# 1. 直接传入列表的方式\n",
    "t1 = np.array([1, 2, 3])\n",
    "print(t1)\n",
    "print(type(t1))\n",
    "'''\n",
    "[1 2 3]\n",
    "<class 'numpy.ndarray'>\n",
    "'''\n",
    "\n",
    "# 2. 传入range生成序列\n",
    "t2 = np.array(range(10))\n",
    "print(t2)\n",
    "print(type(t2))\n",
    "'''\n",
    "[0 1 2 3 4 5 6 7 8 9]\n",
    "<class 'numpy.ndarray'>\n",
    "'''\n",
    "\n",
    "# 3. 使用numpy自带的np.arange()生成数组\n",
    "t3 = np.arange(0, 10, 2)\n",
    "print(t3)\n",
    "print(type(t3))\n",
    "'''\n",
    "[0 2 4 6 8]\n",
    "<class 'numpy.ndarray'>\n",
    "'''"
   ],
   "id": "976fc8b59de9e619",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "<class 'numpy.ndarray'>\n",
      "[1 2 3 4]\n",
      "[1 2 3]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 2 4 6 8]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"\\n[0 2 4 6 8]\\n<class 'numpy.ndarray'>\\n\""
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.2 创建二维数组",
   "id": "69c790643ce6f5e3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:49:30.122433Z",
     "start_time": "2025-03-04T08:49:30.109037Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list2 = [[1, 2], [3, 4], [5, 6]]\n",
    "twoArray = np.array(list2)\n",
    "print(twoArray)"
   ],
   "id": "a482c9a5c144bfed",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.3 常用属性",
   "id": "a0b5e1d39793989f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:49:30.139862Z",
     "start_time": "2025-03-04T08:49:30.128942Z"
    }
   },
   "cell_type": "code",
   "source": [
    "twoArray = np.array(list2)\n",
    "# 获取数组的维度( 注意： 与函数的参数很像)\n",
    "print(twoArray.ndim)\n",
    "# 形状（行，列）\n",
    "print(twoArray.shape)\n",
    "# 有多少个元素\n",
    "print(twoArray.size)"
   ],
   "id": "ce079e3b348fb66",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 2)\n",
      "6\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.4 调整形状",
   "id": "cdfc14556ecdf4ca"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:49:30.152212Z",
     "start_time": "2025-03-04T08:49:30.141867Z"
    }
   },
   "cell_type": "code",
   "source": [
    "four = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "\n",
    "# 修改的是原有的\n",
    "print(f'four:{four}, id(four):{id(four)}')\n",
    "four.shape = (3, 2)\n",
    "print(f'four:{four}, id(four):{id(four)}')\n",
    "\n",
    "# 返回一个新的数组\n",
    "four1 = four.reshape(3, 2)\n",
    "print(f'four1:{four1}, id(four1):{id(four1)}')"
   ],
   "id": "1d58635636540db7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "four:[[1 2 3]\n",
      " [4 5 6]], id(four):2023501579632\n",
      "four:[[1 2]\n",
      " [3 4]\n",
      " [5 6]], id(four):2023501579632\n",
      "four1:[[1 2]\n",
      " [3 4]\n",
      " [5 6]], id(four1):2023501580112\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:49:30.164588Z",
     "start_time": "2025-03-04T08:49:30.154217Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将多维变成一维数组\n",
    "five = four.reshape((6,))\n",
    "# 默认情况下‘C’以行为主的顺序展开，‘F’（Fortran风格）意味着以列的顺序展开\n",
    "six = four.flatten(order='F')\n",
    "print(five)\n",
    "print(six)"
   ],
   "id": "693617ffc7657ddb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n",
      "[1 3 5 2 4 6]\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:49:30.175083Z",
     "start_time": "2025-03-04T08:49:30.167120Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 拓展：数组的形状\n",
    "t = np.arange(24)\n",
    "print(t)\n",
    "print(t.shape)\n",
    "# 转换成二维\n",
    "t1 = t.reshape((4, 6))\n",
    "print(t1)\n",
    "print(t1.shape)"
   ],
   "id": "26e70047cd3088a1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "(24,)\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "(4, 6)\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.5 将数组转成list",
   "id": "cb6b855c2434bb57"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:49:35.108680Z",
     "start_time": "2025-03-04T08:49:30.177290Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将数组转成list\n",
    "a = np.array([9, 12, 88, 14, 25])\n",
    "list_a = a.tolist()\n",
    "print(list_a)\n",
    "print(type(list_a))"
   ],
   "id": "9b4e6d3a77b281fb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9, 12, 88, 14, 25]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 5 NumPy的数据类型",
   "id": "400218b88642fbd5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:49:35.126685Z",
     "start_time": "2025-03-04T08:49:35.112683Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 返回数组中每个元素的字节单位长度，dtype设置数据类型\n",
    "f = np.array([1, 2, 3, 4, 5], dtype=np.int16)\n",
    "print(f.itemsize)  # 1 np.int8(一个字节)\n",
    "# 获取数据类型\n",
    "print(f.dtype)"
   ],
   "id": "69ed2a50859aa47d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "int16\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:49:35.142904Z",
     "start_time": "2025-03-04T08:49:35.128693Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 调整数据类型\n",
    "f1 = f.astype(np.int64)\n",
    "print(f1.dtype)\n",
    "\n",
    "# 拓展随机生成小数\n",
    "# 使用python语法，保留两位\n",
    "print(round(random.random(), 2))\n",
    "\n",
    "arr = np.array([random.random() for i in range(10)])\n",
    "# 取小数点后两位\n",
    "print(np.round(arr, 2))\n",
    "print(arr.dtype)"
   ],
   "id": "35ee1e2b076dfbdb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int64\n",
      "0.7\n",
      "[0.22 0.97 0.49 0.87 0.63 0.07 0.24 0.72 0.06 0.88]\n",
      "float64\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 6 数组的运算",
   "id": "cfb4563e127b89ec"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 6.1数组和数的计算",
   "id": "8a4cadcf24476bc0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:49:35.161544Z",
     "start_time": "2025-03-04T08:49:35.146417Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape((6, 4))\n",
    "print(t1 + 2)\n",
    "print(\"-\" * 20)\n",
    "print(t1 * 2)\n",
    "print(\"-\" * 20)\n",
    "print(t1 / 2)"
   ],
   "id": "f7c2803d892ee392",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "--------------------\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "--------------------\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 6.2 数组和数组的计算",
   "id": "e6a5fc3f336bae5d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:49:35.192461Z",
     "start_time": "2025-03-04T08:49:35.175083Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape((6, 4))\n",
    "t2 = np.arange(100, 124).reshape((6, 4))\n",
    "print(t1 + t2)\n",
    "print(\"-\" * 50)\n",
    "print(t1 - t2)"
   ],
   "id": "c1218d1d6ad56a25",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[100 102 104 106]\n",
      " [108 110 112 114]\n",
      " [116 118 120 122]\n",
      " [124 126 128 130]\n",
      " [132 134 136 138]\n",
      " [140 142 144 146]]\n",
      "--------------------------------------------------\n",
      "[[-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]]\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:49:37.243140Z",
     "start_time": "2025-03-04T08:49:35.199966Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 形状不一样的数组不能进行运算\n",
    "t1 = np.arange(24).reshape((4, 6))\n",
    "t2 = np.arange(18).reshape((3, 6))\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1 - t2)\n",
    "'''\n",
    "ValueError: operands could not be broadcast together with shapes (4,6) (3,6)\n",
    "'''"
   ],
   "id": "ff00b5c3a4679dbc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (4,6) (3,6) ",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[14], line 6\u001B[0m\n\u001B[0;32m      4\u001B[0m \u001B[38;5;28mprint\u001B[39m(t1)\n\u001B[0;32m      5\u001B[0m \u001B[38;5;28mprint\u001B[39m(t2)\n\u001B[1;32m----> 6\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[43mt1\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m-\u001B[39;49m\u001B[43m \u001B[49m\u001B[43mt2\u001B[49m)\n\u001B[0;32m      7\u001B[0m \u001B[38;5;124;03m'''\u001B[39;00m\n\u001B[0;32m      8\u001B[0m \u001B[38;5;124;03mValueError: operands could not be broadcast together with shapes (4,6) (3,6)\u001B[39;00m\n\u001B[0;32m      9\u001B[0m \u001B[38;5;124;03m'''\u001B[39;00m\n",
      "\u001B[1;31mValueError\u001B[0m: operands could not be broadcast together with shapes (4,6) (3,6) "
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:09.659769Z",
     "start_time": "2025-03-04T08:50:09.649260Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组尺寸不一样，其中一个数组的维度为1，可以广播\n",
    "t1 = np.arange(24).reshape((4, 6))\n",
    "t2 = np.arange(0, 6)\n",
    "print(t1)\n",
    "print(t2)\n",
    "print('-' * 50)\n",
    "print(t1 + t2)"
   ],
   "id": "52637c00d56f1178",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[0 1 2 3 4 5]\n",
      "--------------------------------------------------\n",
      "[[ 0  2  4  6  8 10]\n",
      " [ 6  8 10 12 14 16]\n",
      " [12 14 16 18 20 22]\n",
      " [18 20 22 24 26 28]]\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:10.099563Z",
     "start_time": "2025-03-04T08:50:10.088547Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape((4, 6))\n",
    "t2 = np.arange(4).reshape((4, 1))\n",
    "print(t1)\n",
    "print(t2)\n",
    "print('-' * 50)\n",
    "print(t1 - t2)"
   ],
   "id": "dbf370d46b5bed66",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [3]]\n",
      "--------------------------------------------------\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 5  6  7  8  9 10]\n",
      " [10 11 12 13 14 15]\n",
      " [15 16 17 18 19 20]]\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 6.3 数组中的轴",
   "id": "f8ec5904e6391960"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:10.147887Z",
     "start_time": "2025-03-04T08:50:10.139639Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(np.sum(a, axis=0))  # [5 7 9]\n",
    "print(np.sum(a, axis=1))  # [ 6 21]\n",
    "print(np.sum(a))"
   ],
   "id": "40ba580ce1cac436",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n",
      "[ 6 15]\n",
      "21\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:10.313867Z",
     "start_time": "2025-03-04T08:50:10.306565Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24)\n",
    "print(t.shape)\n",
    "# 转换成二维\n",
    "t1 = t.reshape((2, 3, 4))\n",
    "print(t1)  # 最外面的方括号是0轴，第二个方括号是1轴，第三个方括号是2轴"
   ],
   "id": "fcbc8747d75387c1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(24,)\n",
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:10.637141Z",
     "start_time": "2025-03-04T08:50:10.460782Z"
    }
   },
   "cell_type": "code",
   "source": [
    "b = np.sum(t1, axis=0)\n",
    "print('-' * 50)\n",
    "print(f'b:{b.shape}')\n",
    "c = np.sum(t1, axis=1)\n",
    "print('-' * 50)\n",
    "print(f'c:{c.shape}')\n",
    "d = np.sum(t1, axis=2)\n",
    "print('-' * 50)\n",
    "print(f'd:{d.shape}')"
   ],
   "id": "7f2db66be52e76d0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "b:(3, 4)\n",
      "--------------------------------------------------\n",
      "c:(2, 4)\n",
      "--------------------------------------------------\n",
      "d:(2, 3)\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "a76c6b8961e79ee8"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 7 数组的索引和切片",
   "id": "d5c9d3f430b30bbf"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:10.674592Z",
     "start_time": "2025-03-04T08:50:10.667791Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(10)\n",
    "# 冒号分隔切片的参数 start:stop:step 来进行切片操作print(a[2:7:2])# 从索引 2 开始到索引 7 停止，每隔 2 个取一个\n",
    "\n",
    "# 如果只放置一个参数，如 [2]，将返回与该索引相对应的单个元素\n",
    "print(a[2], a)\n",
    "# 如果为 [2:]，表示从该索引开始以后的所有项都将被提取\n",
    "print(a[2:])"
   ],
   "id": "edd752a1e032ea31",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 [0 1 2 3 4 5 6 7 8 9]\n",
      "[2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:10.949165Z",
     "start_time": "2025-03-04T08:50:10.931191Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape(4, 6)\n",
    "print(t1)\n",
    "print('*' * 20)\n",
    "print(t1[1])  # 取一行(一行代表是一条数据，索引也是从0开始的) print(t1[1,:]) # 取一行\n",
    "print('*' * 20)\n",
    "print(t1[1:])  # 取连续的多行\n",
    "print('*' * 20)\n",
    "print(t1[1:3, :])  # 取连续的多行\n",
    "print('*' * 20)\n",
    "print(t1[[0, 2, 3]])  # 取不连续的多行\n",
    "print('*' * 20)\n",
    "print(t1[[0, 2, 3], :])  # 取不连续的多行\n",
    "print('*' * 20)\n",
    "print(t1[:, 1])  # 取一列\n",
    "print('*' * 20)\n",
    "print(t1[:, 1:])  # 连续的多列\n",
    "print('*' * 20)\n",
    "print(t1[:, [0, 2, 3]])  # 取不连续的多列\n",
    "print('*' * 20)\n",
    "print(t1[2, 3])  # # 取某一个值,三行四列\n",
    "print('*' * 20)\n",
    "print(t1[[0, 2], [1, 3]])  # 取不连续的多行和列\n",
    "print('*' * 20)\n",
    "print(t1[1:3, 1:3])  # 取连续的多行和列\n",
    "print('*' * 20)"
   ],
   "id": "5423e0f90c70c4f2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[ 6  7  8  9 10 11]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "********************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[ 1  7 13 19]\n",
      "********************\n",
      "[[ 1  2  3  4  5]\n",
      " [ 7  8  9 10 11]\n",
      " [13 14 15 16 17]\n",
      " [19 20 21 22 23]]\n",
      "********************\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n",
      "********************\n",
      "15\n",
      "********************\n",
      "[ 1 15]\n",
      "********************\n",
      "[[ 7  8]\n",
      " [13 14]]\n",
      "********************\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 8 数组中的数值修改",
   "id": "84c20def935d7b0b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:11.157635Z",
     "start_time": "2025-03-04T08:50:11.148171Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "# # 修改某一行的值\n",
    "# t[1, :] = 0\n",
    "#\n",
    "# # 修改某一列的值\n",
    "# t[:, 1] = 0\n",
    "#\n",
    "# # 修改连续多行\n",
    "# t[1:3, :] = 0\n",
    "#\n",
    "# # 修改连续多列\n",
    "# t[:, 1:4] = 0\n",
    "#\n",
    "# # 修改多行多列，取第二行到第四行，第三列到第五列\n",
    "# t[1:4,2:5]=0\n",
    "#\n",
    "# # 修改多个不相邻的点\n",
    "# t[[0,1],[0,3]]=0\n",
    "# print(t)\n",
    "# 可以根据条件修改，比如讲小于 10 的值改掉\n",
    "# t[t < 10] = 0\n",
    "# print(t)\n",
    "# 使用逻辑判断\n",
    "# np.logical_and & # np.logical_or |\n",
    "# np.logical_not ~\n",
    "# t[(t > 2) & (t < 6)] = 0 # 与\n",
    "# t[(t < 2) | (t > 6)] = 0 # 或\n",
    "# t[~(t > 6)] = 0 # 非\n",
    "print(t)\n"
   ],
   "id": "ac4d33eabce77392",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:11.304642Z",
     "start_time": "2025-03-04T08:50:11.296526Z"
    }
   },
   "cell_type": "code",
   "source": "t < 10",
   "id": "ac70f79d3c9fa59f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True,  True,  True,  True],\n",
       "       [ True,  True,  True,  True, False, False],\n",
       "       [False, False, False, False, False, False],\n",
       "       [False, False, False, False, False, False]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:11.461171Z",
     "start_time": "2025-03-04T08:50:11.452439Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "t = t.clip(10, 18)  # clip(min, max) 限制数组中的元素在 min 和 max 之间\n",
    "print(t)"
   ],
   "id": "ad0313ad35494e3b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10 10 10 10 10 10]\n",
      " [10 10 10 10 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 18 18 18 18 18]]\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:11.602865Z",
     "start_time": "2025-03-04T08:50:11.594248Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# # 拓 展\n",
    "# # 三目运算（ np.where(condition, x, y)满足条件(condition)，输出 x，不满足输出y。)）\n",
    "score = np.array([[80, 88], [82, 81], [75, 81]])\n",
    "result = np.where(score > 80, True, False)\n",
    "print(result)"
   ],
   "id": "2cb85a3644ba0a04",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False  True]\n",
      " [ True  True]\n",
      " [False  True]]\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 9 数组的添加、删除和去重",
   "id": "24b3ec01c0f1a0ec"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 9.1 数组的添加",
   "id": "2ae049de64204a5e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:11.758772Z",
     "start_time": "2025-03-04T08:50:11.746232Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print(' 向 数 组 添 加 元 素 ：')\n",
    "print(np.append(a, [7, 8, 9]))\n",
    "print('\\n')\n",
    "\n",
    "print('沿轴 0 添加元素：')\n",
    "print(np.append(a, [[7, 8, 9]], axis=0))  # 轴是谁，谁就变\n",
    "print('\\n')\n",
    "\n",
    "print('沿轴 1 添加元素：')\n",
    "print(np.append(a, [[5, 5, 5], [7, 8, 9]], axis=1))\n",
    "\n"
   ],
   "id": "3c5d44da91f3311e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "\n",
      " 向 数 组 添 加 元 素 ：\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "\n",
      "\n",
      "沿轴 0 添加元素：\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "\n",
      "\n",
      "沿轴 1 添加元素：\n",
      "[[1 2 3 5 5 5]\n",
      " [4 5 6 7 8 9]]\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:11.891145Z",
     "start_time": "2025-03-04T08:50:11.879584Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2. numpy.insert\n",
    "# 函数在给定索引之前，沿给定轴在输入数组中插入值。# 如果值的类型转换为要插入，则它与输入数组不同。\n",
    "# 插入没有原地的，函数会返回一个新数组。 此外，如果未提供轴，则输入数组会被展开。\n",
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('未传递 Axis 参数。 在插入之前输入数组会被展开。')\n",
    "print(np.insert(a, 3, [11, 12]))\n",
    "print('\\n')\n",
    "\n",
    "print('传递了 Axis 参数。 会广播值数组来配输入数组。')\n",
    "print('沿轴 0 广播：')\n",
    "print(np.insert(a, 1, [11], axis=0))\n",
    "print('\\n')\n",
    "\n",
    "print('沿轴 1 广播：')\n",
    "print(np.insert(a, 1, 11, axis=1))"
   ],
   "id": "99c8d760749d0e96",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在插入之前输入数组会被展开。\n",
      "[ 1  2  3 11 12  4  5  6]\n",
      "\n",
      "\n",
      "传递了 Axis 参数。 会广播值数组来配输入数组。\n",
      "沿轴 0 广播：\n",
      "[[ 1  2]\n",
      " [11 11]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "\n",
      "\n",
      "沿轴 1 广播：\n",
      "[[ 1 11  2]\n",
      " [ 3 11  4]\n",
      " [ 5 11  6]]\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 9.2 数组中的删除",
   "id": "761e085ead394eb4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:12.073496Z",
     "start_time": "2025-03-04T08:50:12.065253Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(12).reshape(3, 4)\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('未传递 Axis 参数。 在删除之前输入数组会被展开。')\n",
    "print(np.delete(a, 5))\n",
    "print('\\n')\n",
    "\n",
    "print('删除每一行中的第二列：')\n",
    "print(np.delete(a, 1, axis=1))\n",
    "print('\\n')"
   ],
   "id": "74ffcbac0fa3c7b5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在删除之前输入数组会被展开。\n",
      "[ 0  1  2  3  4  6  7  8  9 10 11]\n",
      "\n",
      "\n",
      "删除每一行中的第二列：\n",
      "[[ 0  2  3]\n",
      " [ 4  6  7]\n",
      " [ 8 10 11]]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 9.3 数组的去重",
   "id": "619546f99900b01"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:12.158302Z",
     "start_time": "2025-03-04T08:50:12.144615Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([5, 2, 6, 2, 7, 5, 6, 8, 2, 9])\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('第一个数组的去重值：')\n",
    "u = np.unique(a)\n",
    "print(u)\n",
    "print('\\n')\n",
    "\n"
   ],
   "id": "c809320c6e259a00",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "\n",
      "\n",
      "第一个数组的去重值：\n",
      "[2 5 6 7 8 9]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:12.412756Z",
     "start_time": "2025-03-04T08:50:12.399558Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('去重数组的索引数组：')\n",
    "u, indices = np.unique(a, return_index=True)\n",
    "print(indices)\n",
    "print('\\n')\n",
    "\n",
    "print('我们可以看到每个和原数组下标对应的数值：')\n",
    "print(a)\n",
    "print('去重数组的下标：')\n",
    "u, indices = np.unique(a, return_inverse=True)\n",
    "print(u)\n",
    "print(indices)\n",
    "print('\\n')\n",
    "\n",
    "print('返回去重元素的重复数量：')\n",
    "u, indices = np.unique(a, return_counts=True)\n",
    "print(u)\n",
    "print(indices)"
   ],
   "id": "e5637a0350b2c4e6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "去重数组的索引数组：\n",
      "[1 0 2 4 7 9]\n",
      "\n",
      "\n",
      "我们可以看到每个和原数组下标对应的数值：\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "去重数组的下标：\n",
      "[2 5 6 7 8 9]\n",
      "[1 0 2 0 3 1 2 4 0 5]\n",
      "\n",
      "\n",
      "返回去重元素的重复数量：\n",
      "[2 5 6 7 8 9]\n",
      "[3 2 2 1 1 1]\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 10 numpy的计算（数学函数）",
   "id": "bf2ac6d1ec52867f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:12.481629Z",
     "start_time": "2025-03-04T08:50:12.470372Z"
    }
   },
   "cell_type": "code",
   "source": [
    "score = np.array([[80, 88], [82, 81], [75, 81]])\n",
    "# 1. 获取所有数据最大值\n",
    "result = np.max(score)\n",
    "print(result)\n",
    "\n",
    "# 2. 获取某一个轴上的数据最大值\n",
    "result = np.max(score, axis=0)\n",
    "print(result)\n",
    "\n",
    "# 3. 获取最小值\n",
    "result = np.min(score)\n",
    "print(result)\n",
    "\n",
    "# 4. 获取某一个轴上的数据最小值\n",
    "result = np.min(score, axis=0)\n",
    "print(result)\n",
    "\n"
   ],
   "id": "d9a0461e835f5c06",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "88\n",
      "[82 88]\n",
      "75\n",
      "[75 81]\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:50:12.686716Z",
     "start_time": "2025-03-04T08:50:12.662226Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 5. 数据的比较\n",
    "result = np.maximum([-2, -1, 0, 1, 2], 0)  # 第一个参数中的每一个数与第二个参数比较返回大的\n",
    "print(result)\n",
    "result = np.minimum([-2, -1, 0, 1, 2], 0)  # 第一个参数中的每一个数与第二个参数比较返回小的\n",
    "print(result)\n",
    "result = np.maximum([-2, -1, 0, 1, 2], [1, 2, 3, 4, 5])\n",
    "print(result)\n",
    "\n",
    "# 接受的两个参数，也可以大小一致;第二个参数只是一个单独的值时，其实是用到了维度的广播机制；\n",
    "# 6. 求平均值\n",
    "result = np.mean(score)  # 获取所有数据的平均值\n",
    "print(result)\n",
    "result = np.mean(score, axis=0)  # 获取某一行或者某一列的平均值\n",
    "print(result)\n",
    "\n",
    "# 7. 求前缀和\n",
    "arr = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(arr)\n",
    "print(arr.cumsum(0))\n",
    "'''\n",
    "[1, 2, 3]------> |1 |2 |3 |\n",
    "[4, 5, 6]------> |5=1+4 |7=2+5 |9=3+6|\n",
    "'''\n",
    "print(arr.cumsum(1))\n",
    "'''\n",
    "[1, 2, 3]------> |1 |2+1 |3+2+1 |\n",
    "[4, 5, 6]------> |4 |4+5 |4+5+6 |\n",
    "'''\n",
    "# 8. argmin 求最小值索引\n",
    "result = np.argmin(score, axis=0)\n",
    "res = np.min(score, axis=0)  #这样我们就可以知道最小的 81 是第二排的，是从前往后遍历的\n",
    "print(result, res)\n",
    "\n",
    "# 9. 求每一列的标准差（这里是总体标准差）\n",
    "# 标准差是一组数据平均值分散程度的一种度量。一个较大的标准差，代表大部分数值和其平均值之间差异较大；\n",
    "# 一个较小的标准差，代表这些数据较接近平均值反应出数据的波动稳定情况，越大表示波动越大，越不稳定。\n",
    "result = np.std(score, axis=0)\n",
    "print(result)\n",
    "\n",
    "# 10. 极 值\n",
    "result = np.ptp(score, axis=None)  #就是最大值和最小值的差\n",
    "print(result)\n",
    "# 拓展：方差 var, 协方差 cov, 计算平均值 average, 计算中位数 median"
   ],
   "id": "682de2c11417fee3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0 1 2]\n",
      "[-2 -1  0  0  0]\n",
      "[1 2 3 4 5]\n",
      "81.16666666666667\n",
      "[79.         83.33333333]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 2 3]\n",
      " [5 7 9]]\n",
      "[[ 1  3  6]\n",
      " [ 4  9 15]]\n",
      "[2 1] [75 81]\n",
      "[2.94392029 3.29983165]\n",
      "13\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T09:17:05.693076Z",
     "start_time": "2025-03-04T09:17:05.614091Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(np.log(10))  # np.log(x) 计算 x 的自然对数\n",
    "print(np.log2(10))  # np.log2(x) 计算 x 的二进制对数\n",
    "print(np.log10(10))  # np.log10(x) 计算 x 的常用对数"
   ],
   "id": "cc726eeb3c1e5aeb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.302585092994046\n",
      "3.321928094887362\n",
      "1.0\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 11 数组的拼接与分割",
   "id": "163bab00a898b1b1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T09:23:01.180017Z",
     "start_time": "2025-03-04T09:23:01.169947Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1. 根据轴连接的数组序列\n",
    "a = np.array([[1, 2], [3, 4]])\n",
    "b = np.array([[5, 6], [7, 8]])\n",
    "\n",
    "# 要求 a,b 两个数组的维度相同\n",
    "print('沿轴 0 连接两个数组：')\n",
    "print(np.concatenate((a, b), axis=0))\n",
    "print('\\n')\n",
    "print('沿轴 1 连接两个数组：')\n",
    "print(np.concatenate((a, b), axis=1))\n",
    "\n"
   ],
   "id": "7586ee38b5a65c74",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 0 连接两个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n",
      "\n",
      "\n",
      "沿轴 1 连接两个数组：\n",
      "[[1 2 5 6]\n",
      " [3 4 7 8]]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T09:22:48.214874Z",
     "start_time": "2025-03-04T09:22:48.203877Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2. 根据轴进行堆叠\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "b = np.arange(12).reshape(3, 4)\n",
    "print('沿轴 0 连接两个数组：')\n",
    "print(np.stack((a, b), axis=0).shape)\n",
    "print('\\n')\n",
    "print('沿轴 1 连接两个数组：')\n",
    "print(np.stack((a, b), axis=1).shape)"
   ],
   "id": "727bf71050fad90c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 0 连接两个数组：\n",
      "(2, 3, 4)\n",
      "\n",
      "\n",
      "沿轴 1 连接两个数组：\n",
      "(3, 2, 4)\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T09:24:33.179868Z",
     "start_time": "2025-03-04T09:24:33.168031Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 不重要的例子\n",
    "# 3. 矩阵垂直拼接\n",
    "v1 = [[0, 1, 2, 3, 4, 5],\n",
    "      [6, 7, 8, 9, 10, 11]]\n",
    "v2 = [[12, 13, 14, 15, 16, 17],\n",
    "      [18, 19, 20, 21, 22, 23]]\n",
    "result = np.vstack((v1, v2))\n",
    "print(result)\n",
    "\n",
    "# 4. 矩阵水平拼接\n",
    "v1 = [[0, 1, 2, 3, 4, 5],\n",
    "      [6, 7, 8, 9, 10, 11]]\n",
    "v2 = [[12, 13, 14, 15, 16, 17],\n",
    "      [18, 19, 20, 21, 22, 23]]\n",
    "result = np.hstack((v1, v2))\n",
    "print(result)"
   ],
   "id": "777cf322b5896c85",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[ 0  1  2  3  4  5 12 13 14 15 16 17]\n",
      " [ 6  7  8  9 10 11 18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T09:26:59.061996Z",
     "start_time": "2025-03-04T09:26:59.046738Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#  数组的分割\n",
    "arr = np.arange(9).reshape(3, 3)\n",
    "print('原始数组：')\n",
    "print(arr)\n",
    "print('\\n')\n",
    "\n",
    "print('沿轴 0 分割数组：')\n",
    "a, b = np.split(arr, [1], axis=0)\n",
    "print(a)\n",
    "print(b)\n",
    "print('\\n')\n",
    "\n",
    "print('沿轴 1 分割数组：')\n",
    "a, b = np.split(arr, [1], axis=1)\n",
    "print(a)\n",
    "print(b)\n",
    "print('\\n')\n",
    "\n",
    "print('将数组分为三个大小相等的子数组：')\n",
    "b = np.split(arr, 3)\n",
    "print(b)"
   ],
   "id": "33254a8ade5df7b3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n",
      "\n",
      "\n",
      "沿轴 0 分割数组：\n",
      "[[0 1 2]]\n",
      "[[3 4 5]\n",
      " [6 7 8]]\n",
      "\n",
      "\n",
      "沿轴 1 分割数组：\n",
      "[[0]\n",
      " [3]\n",
      " [6]]\n",
      "[[1 2]\n",
      " [4 5]\n",
      " [7 8]]\n",
      "\n",
      "\n",
      "将数组分为三个大小相等的子数组：\n",
      "[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T09:32:05.532705Z",
     "start_time": "2025-03-04T09:32:05.458630Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2.numpy.hsplit 函数用于水平分割数组，通过指定要返回的相同形状的数组数量来拆分原数组。\n",
    "harr = np.floor(10 * np.random.random((2, 6)))  # floor函数用于向下取整，ceil函数用于向上取整,round函数用于四舍五入\n",
    "print('原始数组：')\n",
    "print(harr)\n",
    "print('拆分后：')\n",
    "print(np.hsplit(harr, 3))\n",
    "\n",
    "# 3.numpy.vsplit 沿着垂直轴分割\n",
    "a = np.arange(16).reshape(4, 4)\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print('竖直分割：')\n",
    "b = np.vsplit(a, 2)\n",
    "print(b)"
   ],
   "id": "9fdd5f55af8f5232",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[8. 3. 3. 6. 4. 8.]\n",
      " [4. 9. 4. 9. 5. 0.]]\n",
      "拆分后：\n",
      "[array([[8., 3.],\n",
      "       [4., 9.]]), array([[3., 6.],\n",
      "       [4., 9.]]), array([[4., 8.],\n",
      "       [5., 0.]])]\n",
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n",
      "\n",
      "\n",
      "竖直分割：\n",
      "[array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],\n",
      "       [12, 13, 14, 15]])]\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 12 nan和inf的处理",
   "id": "cd661ae5f9f14cab"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# nan 不是数字，是用来表示缺失或无效数据的特殊值。Not a Number。\n",
    "# inf 无穷大，表示无限大的数值。Infinity。"
   ],
   "id": "7a75878e9dd46096"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T09:34:10.319398Z",
     "start_time": "2025-03-04T09:34:10.306193Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a, type(a), b, type(b))"
   ],
   "id": "9cdf4caef64aff25",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'> inf <class 'float'>\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T09:35:14.792798Z",
     "start_time": "2025-03-04T09:35:14.778958Z"
    }
   },
   "cell_type": "code",
   "source": "np.nan == np.nan  # nan 与 nan 相等\n",
   "id": "b1876ffeef3ded90",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T09:35:30.194526Z",
     "start_time": "2025-03-04T09:35:30.187519Z"
    }
   },
   "cell_type": "code",
   "source": "np.inf == np.inf",
   "id": "182faa014b0343f9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T09:37:48.624658Z",
     "start_time": "2025-03-04T09:37:48.614157Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# --判断数组中为 nan 的个数\n",
    "t = np.arange(24, dtype=float).reshape(4, 6)\n",
    "print(t)\n",
    "# 将三行四列的数改成 nan\n",
    "t[3, 4] = np.nan\n",
    "# 可以使用 np.count_nonzero() 来判断非零的个数\n",
    "print(np.count_nonzero(t))\n",
    "# 并 且 np.nan != np.nan 结果 是 TRUE\n",
    "# 所以我们可以使用这两个结合使用判断 nan 的个数\n",
    "print(np.count_nonzero(t != t))  # t != t 结果是 nan 元素的布尔值数组 true or false       \n",
    "# 将 nan 替换为 0\n",
    "t[np.isnan(t)] = 0\n",
    "print(t)"
   ],
   "id": "fa611dd0f409f9b0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "23\n",
      "1\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21.  0. 23.]]\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T09:50:09.478497Z",
     "start_time": "2025-03-04T09:50:09.466009Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6).astype('float')\n",
    "#\n",
    "# 将数组中的一部分替换 nan\n",
    "t[1, 3:] = np.nan\n",
    "print(t)\n",
    "print('-------------------')\n",
    "# 遍历每一列，然后判断每一列是否有 nan\n",
    "for i in range(t.shape[1]):\n",
    "    #获取当前列数据\n",
    "    temp_col = t[:, i]\n",
    "    # 判断当前列的数据中是否含有 nan\n",
    "    nan_num = np.count_nonzero(temp_col != temp_col)\n",
    "    # 条件成立说明含有 nan\n",
    "    if nan_num != 0:\n",
    "        # 将这一列不为 nan 的数据拿出来\n",
    "        temp_col_not_nan = temp_col[temp_col == temp_col]\n",
    "        # 将 nan 替换成这一列的平均值\n",
    "        temp_col[np.isnan(temp_col)] = np.mean(temp_col_not_nan)\n",
    "print(t)"
   ],
   "id": "82f28e5c4e8131c4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. nan nan nan]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "-------------------\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. 13. 14. 15.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 13 转置和轴滚动",
   "id": "79fcbc509c541ea"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T09:52:43.447598Z",
     "start_time": "2025-03-04T09:52:43.437161Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(12).reshape(3, 4)\n",
    "print('原数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('对换数组：')\n",
    "print(np.transpose(a))\n",
    "print('\\n')\n",
    "\n",
    "# 与 transpose 一致\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "print('原数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('转置数组：')\n",
    "print(a.T)"
   ],
   "id": "bc16284a273aed97",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "对换数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n",
      "\n",
      "\n",
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "转置数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T10:02:35.435045Z",
     "start_time": "2025-03-04T10:02:35.420321Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 函数用于交换数组的两个轴\n",
    "t1 = np.arange(24).reshape(4, 6)\n",
    "re = t1.swapaxes(1, 0)\n",
    "\n",
    "print(' 原 数 组 ：')\n",
    "print(t1)\n",
    "print('\\n')\n",
    "print('调用 swapaxes 函数后的数组：')\n",
    "print(re)"
   ],
   "id": "c67519fd0193a606",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 原 数 组 ：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "\n",
      "调用 swapaxes 函数后的数组：\n",
      "[[ 0  6 12 18]\n",
      " [ 1  7 13 19]\n",
      " [ 2  8 14 20]\n",
      " [ 3  9 15 21]\n",
      " [ 4 10 16 22]\n",
      " [ 5 11 17 23]]\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T10:03:19.859139Z",
     "start_time": "2025-03-04T10:03:19.851437Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.ones((3, 4, 5, 6))\n",
    "\n",
    "t1 = np.rollaxis(t, 3, 0)\n",
    "print(t1.shape)"
   ],
   "id": "9c8426291fa25ec9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(6, 3, 4, 5)\n"
     ]
    }
   ],
   "execution_count": 22
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
